package com.dhecp.project.oa.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.common.utils.TypeUtils;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.core.layui.GridColumnsPropertyModel;
import com.dhecp.core.utils.ExportExcelUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.common.controller.BaseModuleController;
import com.dhecp.project.oa.domain.FixedAssets;
import com.dhecp.project.oa.service.FixedAssetsService;
import com.dhecp.project.system.domain.SysDictionaryForExport;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * 固定资产登记 控制层
 * @author lwl
 * @version 2020年7月6日
 */
@Controller
@RequestMapping("/oa/fixedassets")
public class FixedAssetsController extends BaseModuleController{

	private String prefix = "oa/fixedassets";
    private String moduleTable = ModuleTable.FIXED_ASSETS;
    
    @Autowired
    private FixedAssetsService fixedAssetsService;
    
    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;
    
    @Autowired
    private GridColumnsPropertyModel gridColumnsPropertyModel;
    
    /**
     * 打开列表
     * @param 
     * @return ModelAndView
     * @author lwl
     */
    @RequiresPermissions("oa:fixedassets:view")
    @GetMapping()
    public ModelAndView dataList(@RequestParam Map<String, Object> params)
    {
		params.put("moduleTable", moduleTable);
    	params.put("entity", new FixedAssets()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
    	Subject userSubject = ShiroUtils.getSubject();
    	Integer toolBarWidth = 0;
    	if (userSubject.isPermitted("oa:fixedassets:delete")){	//当前登录用户主体是否有删除的权限
        	params.put("checkBox", "yes"); //生成表格gridColumns时包含复选
        	toolBarWidth = toolBarWidth + 100;
    	}
    	if (userSubject.isPermitted("oa:fixedassets:edit")){ //当前登录用户主体是否有修改的权限
    		toolBarWidth = toolBarWidth + 100;
    	}
    	params.put("toolBarWidth", toolBarWidth);
    	

    	Object importFields = null;
    	importFields = dictionaryForFieldService.queryFieldFrontImport(params); //导入模板
    	
		ModelAndView mv = this.getModelAndView();
        AjaxResult ajaxResult = new AjaxResult();
        Map<String, Object> dictionaryForFieldMap = new HashMap<String, Object>();
        try{
            dictionaryForFieldMap = dictionaryForFieldService.queryFieldAttributesMap(params);
            if (dictionaryForFieldMap.isEmpty()){
                ajaxResult = AjaxResult.error("未获取到字段属性。");
            }else{
                ajaxResult = AjaxResult.success("数据获取成功。");
            }
        }
        catch(Exception e){
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        mv.addObject("fields", TypeUtils.mapToJson(dictionaryForFieldMap));
        mv.addObject("ajaxResult", ajaxResult);
        mv.addObject("gridColumns", gridColumnsPropertyModel.queryGridColumnsPropertyModel(params));
        mv.addObject("importFields",importFields);				//导入字段模板
        mv.addObject("moduleTable", moduleTable);
        mv.setViewName(prefix + "/fixedassets");
		return mv;
    }
    
	/**
     * 查询所有信息
     * @param params
     * @return TableDataInfo
     * @author lwl
     */
	@GetMapping("/list")
    @RequiresPermissions("oa:fixedassets:list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, Object> params)
    {
		params.put("moduleTable", moduleTable);
        List<FixedAssets> list = new ArrayList<FixedAssets>();
        try {
        	list = fixedAssetsService.queryAll(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());            
        }
        return getDataTable(list);
    }
	
	/**
     * 导出信息
     * @param params
     * @return AjaxResult
     * @author lwl
	 * @throws Exception 
     */
    @Log(title = "固定资产登记导出", businessType = BusinessType.EXPORT)
    @RequiresPermissions("oa:fixedassets:export")
    @GetMapping("/export")
    @ResponseBody
    public AjaxResult export(@RequestParam Map<String, Object> params) throws Exception{
		params.put("moduleTable", moduleTable);
    	params.put("entity", new FixedAssets()); 
        List<FixedAssets> list = fixedAssetsService.queryAll(params);
    	params.put("getFieldAttributesType", "export");
    	List<SysDictionaryForExport> fieldList = dictionaryForFieldService.queryExportFieldList(params);
        ExportExcelUtils<FixedAssets> util = new ExportExcelUtils<FixedAssets>(moduleTable,new FixedAssets());
        return util.exportExcel(list, fieldList, "固定资产登记数据", "export");
    }
    
    /**
     * 导出Excel导入模板
     * @param params
     * @return
     * @throws Exception
     * @author kimiloo
     */
    @RequiresPermissions("oa:fixedassets:import")
    @Log(title = "固定资产登记数据导出", businessType = BusinessType.EXPORT)
    @GetMapping("/exporttemplate")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult exportTemplate(@RequestParam Map<String, Object> params) throws Exception {
		params.put("moduleTable", moduleTable);
    	params.put("entity", new FixedAssets()); 
    	params.put("getFieldAttributesType", "template");

    	List<SysDictionaryForExport> fieldList = dictionaryForFieldService.queryExportFieldList(params);
        ExportExcelUtils<FixedAssets> util = new ExportExcelUtils<FixedAssets>(moduleTable,new FixedAssets());
        return util.exportExcel(null, fieldList, "固定资产登记导入模板", "template");
    }
	
	/**
     * 打开新增信息页面
     * @param params
     * @return ModelAndView
     * @author lwl
     */
    @RequiresPermissions("oa:fixedassets:add")
    @GetMapping("/add")
    public ModelAndView add(@RequestParam Map<String, Object> params)
    {
    	params.put("moduleTable", moduleTable);
    	params.put("entity", new FixedAssets());
        ModelAndView mv = this.getModelAndView();
        AjaxResult ajaxResult = new AjaxResult();
        Map<String, Object> dictionaryForFieldMap = new HashMap<String, Object>();
        try{
            dictionaryForFieldMap = dictionaryForFieldService.queryFieldAttributesMap(params);
            if (dictionaryForFieldMap.isEmpty()){
                ajaxResult = AjaxResult.error("未获取到字段属性。");
            }else{
                ajaxResult = AjaxResult.success("数据获取成功。");
            }
        }
        catch(Exception e){
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        mv.addObject("pageType","add");
        mv.addObject("fields", dictionaryForFieldMap.values());
        mv.addObject("ajaxResult", ajaxResult);
        mv.setViewName(prefix + "/fixedassets_add");
        return mv;
    }
    
    /**
     * 新增信息——数据提交
     * @param params
     * @return AjaxResult
     * @author lwl
     */
    @Log(title = "固定资产保存", businessType = BusinessType.INSERT)
    @RequiresPermissions("oa:fixedassets:add")
    @PostMapping("/addpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult addSave(@RequestParam Map<String, Object> params)
    {
    	AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= fixedAssetsService.insertFixedAssets(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
    
    /**
     * 打开信息修改页面
     * @param params
     * @return ModelAndView
     * @author lwl
     */
    @RequiresPermissions("oa:fixedassets:edit")
    @GetMapping("/edit")
    public ModelAndView talentPoolEdit(@RequestParam Map<String, Object> params){
        params.put("moduleTable", moduleTable);
        params.put("entity", new FixedAssets());
        ModelAndView mv = this.getModelAndView();
        AjaxResult ajaxResult = new AjaxResult();
        Map<String, Object> dictionaryForFieldMap = new HashMap<String, Object>();
        FixedAssets fixedAssets= new FixedAssets();
        mv = this.gotoModelAndViewErrorPage(params, "guid", "唯一编号");
        if (mv.isEmpty()){
            try{
                dictionaryForFieldMap = dictionaryForFieldService.queryFieldAttributesMap(params);
                if (dictionaryForFieldMap.isEmpty()){
                    ajaxResult = AjaxResult.error("未获取到字段属性。");
                }else{
                	fixedAssets = fixedAssetsService.queryByGuid(params.get("guid").toString());
                	if (fixedAssets == null){
                		ajaxResult = AjaxResult.error("没有查到该信息。");
                	}else{
                    	ajaxResult = AjaxResult.success("数据获取成功。");
                	}
                }
            }
            catch(Exception e){
                ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
            }
            mv.addObject("pageType","edit");
            mv.addObject("fixedAssets", TypeUtils.entityToMap(fixedAssets));
            mv.addObject("fields", dictionaryForFieldMap.values());
            mv.addObject("ajaxResult", ajaxResult);
            mv.setViewName(prefix + "/fixedassets_add");
        }
        return mv;
    }
    
    /**
     * 修改信息--数据提交
     * @param params
     * @return AjaxResult
     * @author lwl
     */
    @Log(title = "固定资产修改", businessType = BusinessType.UPDATE)
    @RequiresPermissions("oa:fixedassets:edit")
    @PostMapping("/editpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult editSave(@RequestParam Map<String, Object> params)
    {
    	AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= fixedAssetsService.updateFixedAssets(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
    
    /**
     * 删除数据
     * @param params
     * @return AjaxResult
     * @author lwl
     */
    @RequiresPermissions("oa:fixedassets:delete")
    @Log(title = "删除固定资产", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult delete(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= fixedAssetsService.deleteByGuid(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e)); 
        }
        return ajaxResult;
    }
}
